package com.jerry.alibaba.transaction.controller;

import com.jerry.alibaba.transaction.entity.CommonResult;
import com.jerry.alibaba.transaction.entity.Storage;
import com.jerry.alibaba.transaction.service.StorageService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class StorageController {

    @Resource
    private StorageService storageService;

    @PostMapping(value="/storage/decrease")
    public CommonResult decrease(@RequestParam("productId") Long productId, @RequestParam("count") Integer count) {
        storageService.decrease(productId, count);
        return new CommonResult(200, "扣减库存成功");
    }

    @PostMapping(value="/storage/create")
    public CommonResult create(@RequestBody Storage storage) {
        int result = storageService.create(storage);
        if (result > 0) {
            return new CommonResult(200, "库存创建成功");
        } else {
            return new CommonResult(444, "库存创建失败");
        }

    }

    @GetMapping(value="/storage/get/{id}")
    public CommonResult getById(@PathVariable("id") Long id) {
        Storage storage = storageService.getById(id);
        if (storage != null) {
            return new CommonResult(200, "查询成功", storage);
        } else {
            return new CommonResult(444, "查询失败，查询id="+id);
        }
    }

    @GetMapping(value="/storage/get")
    public CommonResult getList() {
        List<Storage> storageList = storageService.getList();
        if (storageList != null) {
            return new CommonResult(200, "查询成功", storageList);
        } else {
            return new CommonResult(444, "查询失败");
        }
    }

}
